﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Ledict.Utility.Extension
{
    /// <summary>
    /// 正则验证
    /// </summary>
    /// <remarks>
    /// Create Author:Zhangpeng
    /// Create Date:2014-03-10
    /// </remarks>
    public static class RegexExtension
    {
        #region 电话号码
        /// <summary>
        /// 验证符合电话号码的规范
        /// </summary>
        /// <param name="src">需要验证的字符串</param>
        /// <returns>结果</returns>
        public static bool RegexTelephone(this string src)
        {
            if (string.IsNullOrEmpty(src)) return false;
            var result = Regex.IsMatch(src,
                 @"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)");
            return result;
        }
        #endregion

        #region 正整数
        /// <summary>
        /// 验证录入的数据为正整数
        /// </summary>
        /// <param name="control">需要验证的字符串</param>
        /// <returns></returns>
        public static bool RegexNum(this string control)
        {
            if (string.IsNullOrEmpty(control)) return false;
            var result = Regex.IsMatch(control, @"^[1-9]\d*$");
            return result;
        }
        #endregion

        #region 正整数加0
        /// <summary>
        /// 验证录入的数据为正整数
        /// </summary>
        /// <param name="control">需要验证的字符串</param>
        /// <returns></returns>
        public static bool RegexNumWithZero(this string control)
        {
            if (string.IsNullOrEmpty(control)) return false;
            var result = Regex.IsMatch(control, @"^[0-9]\d*$");
            return result;
        }
        #endregion

        #region 包含数字和字母
        /// <summary>
        /// 验证字符串只能包含数字和字母
        /// </summary>
        /// <param name="src">需要验证的字符串</param>
        /// <returns>结果</returns>
        public static bool RegexLetterNum(this string src)
        {
            if (string.IsNullOrEmpty(src)) return false;
            var result = Regex.IsMatch(src,
                 @"^[a-zA-Z0-9_]+$");
            return result;
        }
        #endregion

        #region 正整数或者正浮点数
        /// <summary>
        /// 验证字符串为正整数或者正浮点数
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static bool RegexDouble(this string src)
        {
            if (string.IsNullOrEmpty(src)) return false;
            var result = Regex.IsMatch(src,
                 @"(^[1-9]\d*$)|(^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$)");
            return result;
        }
        #endregion

        #region 自然数或者正浮点数
        /// <summary>
        /// 验证字符串为自然数或者正浮点数
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static bool RegexZeroDouble(this string src)
        {
            if (string.IsNullOrEmpty(src)) return false;
            var result = Regex.IsMatch(src,
                 @"(^[0-9]\d*$)|(^[0-9]\d*\.\d*|0\.\d*[1-9]\d*$)");
            return result;
        }
        #endregion

        #region 身份证号
        /// <summary>
        /// 验证符合身份证号的规范
        /// </summary>
        /// <param name="src">需要验证的字符串</param>
        /// <returns>结果</returns>
        public static bool RegexIDCard(this string src)
        {
            if (string.IsNullOrEmpty(src)) return false;
            var result = Regex.IsMatch(src, @"^\d{15}|\d{18}$");
            return result;
        }
        #endregion

    }
}
